package com.hk.adapter;

import java.util.List;
import org.apache.http.message.BasicNameValuePair;
import com.hk.R;
import com.hk.activity.MainActivity;
import com.hk.activity.Question1Activity;
import com.hk.activity.Question3Activity;
import com.hk.model.Question;
import com.hk.util.Utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 数据适配器
 */
public class QAdapter extends BaseAdapter {

	private Context mContext;
	private List<Question> mLists;
	private LayoutInflater mLayoutInflater;

	public QAdapter(Context pContext, List<Question> pLists) {
		this.mContext = pContext;
		this.mLists = pLists;
		mLayoutInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mLists != null ? mLists.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return mLists.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int p, View view, ViewGroup vg) {
		Holder _Holder = null;
		if (null == view) {
			_Holder = new Holder();
			view = mLayoutInflater.inflate(R.layout.item_question, null);
			_Holder.item_title = (TextView) view.findViewById(R.id.item_tv);
			_Holder.item_btn = (Button) view.findViewById(R.id.item_btn);
			view.setTag(_Holder);

		} else {
			_Holder = (Holder) view.getTag();
		}

		view.setOnClickListener(new ViewListener(mLists.get(p).ID, mLists.get(p).QuestionType, mLists.get(p).Title,p));

		_Holder.item_btn.setText(String.valueOf(p + 1));
		_Holder.item_title.setText(mLists.get(p).Title.substring(0, 80)+"...");
		return view;
	}

	class ViewListener implements OnClickListener {
		int id;
		String type;
		String title;
		int pos;

		ViewListener(int _id, String _type, String _title,int _pos) {
			id = _id;
			type = _type;
			title = _title;
			pos=_pos;
		}

		@Override
		public void onClick(View arg0) {

			setNowQuestion(type, String.valueOf(pos));

			Intent intent = new Intent();
			intent.setClass(mContext, Question3Activity.class);
			Bundle bundle = new Bundle();
			bundle.putString("Pid", String.valueOf(id));
			bundle.putString("PTitle", title);
			bundle.putString("QuestionType", type);
			bundle.putString("Head", "阅读理解");
			intent.putExtras(bundle);
			mContext.startActivity(intent);
		}
	}

	public void setNowQuestion(String mQType, String pos) {
		Utils.putValue(mContext, "Question_" + mQType, pos);
	}

	private class Holder {
		Button item_btn;
		TextView item_title;
	}
}
